/*
 * Project Name: jmax
 * File Name: ReceiveHandler.java
 * Copyright: Copyright(C) 1985-2015 ZKTeco Inc. All rights reserved.
 */
package com.xdtech.hotel.protocol.server;

import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jettison.json.JSONObject;

import com.xdtech.common.utils.ApplicationContextUtil;
import com.xdtech.hotel.protocol.manager.DeviceMonitor;
import com.xdtech.hotel.service.DeviceEventService;
import com.xdtech.hotel.service.DeviceService;
import com.xdtech.hotel.service.impl.DeviceOperationServiceImpl;
import com.xdtech.hotel.util.HotelConstants;
import com.xdtech.hotel.util.RedisUtils;

/**
 * 
 * @version TODO 添加版本
 * @see 相关类或方法，不需要请删除此行
 * @since 2015-4-23 下午2:22:03
 */
public class ReceiveHandler
{
	private static Log log = LogFactory.getLog(ReceiveHandler.class);
	private static Thread handlerPostDataThread = new Thread(new HandlerDevicePostDataRunning(),"HandlerDevicePostDataThread");//设备上传数据处理线程
	public static void handlerEvent(String ip,String content) {
		//此处根据不同的设备上传信息，进行解析分类处理
		try
		{
			JSONObject json = new JSONObject(content);
			String sn = json.getString("SN");
//			JSONArray commit = json.getJSONArray("commit");
			
			if (DeviceMonitor.monitorDevices.get(sn)==null) {
				//注册自动添加设备
				DeviceService deviceService = (DeviceService) ApplicationContextUtil.getContext().getBean("deviceServiceImpl");
				deviceService.addAutoAddDevice(ip,sn);
			}
			//更新设备状态信息操作
			DeviceMonitor.updateRcuDevice(sn, HotelConstants.DEV_STATUS_ONLINE);
			//更新设备最近访问时间
			DeviceMonitor.updateDeviceLastestRequestTime(sn, System.currentTimeMillis());
			/************测试一台RCU演示****/
			DeviceEventService deviceEventService = (DeviceEventService) ApplicationContextUtil.getContext().getBean("deviceEventServiceImpl");
			deviceEventService.handlPostData(content);
			
			//根据不同sn号，存放待处理的内容到redis里
//			RedisUtils.addDevicePostContent(sn,content);
			
			//启动处理线程
//			if(!handlerPostDataThread.isAlive())
//			{
//				if(handlerPostDataThread.getState() == Thread.State.TERMINATED)//如果该线程是否已经结束，则建立新的线程
//				{
//					handlerPostDataThread = new Thread(new HandlerDevicePostDataRunning(),"HandlerDevicePostDataThread");
//				}
//				handlerPostDataThread.start();
//			}
			
		}
		catch (Exception e)
		{
			log.error("设备上传信息处理异常,内容="+content,e);
		}
	}
	
	/**
	 * 
	 * 处理设备上传数据的线程
	 * @version TODO 添加版本
	 * @see 相关类或方法，不需要请删除此行
	 * @since 2015-4-24 下午3:51:31
	 */
	private static class HandlerDevicePostDataRunning implements Runnable
	{
		public void run()
		{
			while (true)
			{
				try
				{
					Set<String> keysSet = RedisUtils.checkExistPostContent();
					String postContent = "";
			 		if (keysSet!=null&&keysSet.size()>0)
					{
						for (String key : keysSet)
						{
							postContent = RedisUtils.getDevicePostDataByKey(key);
							log.info("处理设备上传上来的数据内容.---"+postContent);
							DeviceEventService deviceEventService = (DeviceEventService) ApplicationContextUtil.getContext().getBean("deviceEventServiceImpl");
							deviceEventService.handlPostData(postContent);
						}
					}
//			 		else
//					{
//						Thread.sleep(5000);
//					}
				}
				catch (Exception e)
				{
					log.error("Handler Device Post Content Error", e);
				}
			}
		}
		
	}
}
